$(function () {
    obj.init();
});

var obj = new JsFun();
function JsFun() {
    return {
        dataGrid: null,
        URL: '',
        school_area:null,
        start: null,
        end:null,
        init: function () {
            this.start = $('#repair_startTime').datebox({disabled: true, width: 95});
            this.end = $('#repair_endTime').datebox({disabled: true, width: 95});
            this.dataGrid = new xhDataGrid($('#list'), 'Device/getRepairData');
            this.dataGrid.parseToolbar().init().activeButton(
                ['bt_list_delRepair', 'bt_list_confirmPay', 'bt_list_sendBack', 'bt_list_changeStatus', 'bt_list_takeAway', 'bt_list_startRepair', 'bt_list_startCheck', 'bt_list_goRepair', 'bt_list_receive'],
                ['bt_list_history', 'bt_list_endRepair', 'bt_list_uploadDoc', 'bt_list_delay']);
            this.school_area = $('#school_area');
            $('#bt_list_checkResult').menubutton({
                menu: '#menu_checkResult'
            });
            $('#bt_list_checkPay').menubutton({
                menu: '#menu_checkPay'
            });
            $('#bt_list_repairResult').menubutton({
                menu: '#menu_repairResult'
            });
            $('#bt_list_stat').menubutton({
                menu: '#menu_repairStat'
            });
            $('#bt_list_communication').menubutton({
                menu: '#menu_communication'
            });
            xhExtra.clearLoadSuccess(this.dataGrid);
            this.listener();
        },
        listener: function () {
            var $this = this;
            var $length = 0;
            // //获取区域数据
            $('#school_area').combotree({
                url: 'getDepData',
                animate:true,
                onLoadSuccess:function(node,data){
                    $length = data.length;
                    $.each(data,function (id,value) {
                        if (value.children !== undefined) {
                            $length = $length + value.children.length;
                        }
                    })
                },
                onShowPanel:function(){
                    if ($length <= 8){
                        $(this).combobox('panel').height("auto");//高度自适应
                    }else{
                        $(this).combobox('panel').height("200");//高度自定义
                    }
                },
                onBeforeSelect: function (node) {
                    var t = $(this).tree;
                    var isLeaf = t('isLeaf', node.target);
                    if (!isLeaf) {//选择的不是叶子节点
                        return false;
                    }
                }
            });
            //时间类型
            $('#repair_date').combobox({
                valueField:'value',
                textField:'label',
                data: [{label: '--时间--', value: ''},{label: '送修时间', value: '0'},{label: '最后沟通时间', value: '1'},{label: '寄回时间', value: '2'}],
                onSelect: function(param){
                    if (param.value === '0' || param.value === '1' || param.value === '2'){
                        $('#repair_startTime').datebox({
                            disabled: false
                        });
                        $('#repair_endTime').datebox({
                            disabled: false
                        });
                    } else {
                        $('#repair_startTime').datebox({
                            disabled: true
                        });
                        $('#repair_endTime').datebox({
                            disabled: true
                        });
                    }
                }
            });
            //双击
            this.dataGrid.onDblClickRow = function(rowIndex, rowData){
                var id = rowData['iId'];
                if (id.length <= 0) {
                    window.top.xhTop.showError('未选中记录！');
                    return false;
                }
                window.top.xhTop.showModal({
                    title: '修改信息',
                    content: xhTool.getFrameString('Device/modifyInfo', {id:id}, 480),
                    button: '保存',
                    size: 'md',
                    width:'800'
                });
            };
            //添加
            $('#bt_list_add').click(function(){
                if(xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().addRepair();
            });
            //接收
            $('#bt_list_receive').click(function(){
                if(xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().receiveQR();
            });
            //送修
            $('#bt_list_goRepair').click(function(){
                if(xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().deliverRepair();
            });
            //开始检修
            $('#bt_list_startCheck').click(function(){
                if(xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().startCheck();
            });
            //开始维修
            $('#bt_list_startRepair').click(function(){
                if(xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().startRepair();
            });
            //维修完成
            $('#bt_list_endRepair').click(function(){
                if(xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().endRepair();
            });
            //收费确认
            $('#bt_list_confirmPay').click(function(){
                if(xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().confirmPay();
            });
            //寄回
            $('#bt_list_sendBack').click(function(){
                if(xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().sendBack();
            });
            //查询
            xhTool.bindDocKeyDown(
                $('#bt_list_search').click(function () {
                    $this.funObj().searchList();
                })
            );
            //查看检测报告
            $('.xh-datagrid').on('click','.checkReport',function () {
                var reportUrl=$(this).attr('reportUrl');
                window.top.xhTop.showModal({
                    width:'800',
                    title:'查看检测报告',
                    // content: '<iframe src="'+reportUrl+'" imscrolling="yes" style="width: 100%;border-style: none;padding: 0px;height:600px;"></iframe>'
                    content: '<img style="max-width:100%" src="'+reportUrl+'" alt="无检测报告数据">'
                });
            });
            //查看收费票据
            $('.xh-datagrid').on('click','.document',function () {
                var documentUrl=$(this).attr('documentUrl');
                window.top.xhTop.showModal({
                    width:'800',
                    title:'查看收费票据',
                    // content: '<iframe src="'+documentUrl+'" scrolling="yes" style="width: 100%;border-style: none;padding: 0px;height:600px"></iframe>'
                    content: '<img style="max-width:100%" src="'+documentUrl+'" alt="无收费票据数据">'
                });
            });
            //流程记录
            $('#bt_list_history').click(function(){
                if(xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().history();
            });
            //延时记录
            $('#bt_list_delay').click(function(){
                if(xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().repairDelay();
            });
            //领取
            $('#bt_list_takeAway').click(function(){
                if(xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().bt_list_takeAwayQR();
            });
            //查看故障详情
            $('.xh-datagrid').on('click','.sRemark',function () {
                var sRemark=$(this).attr('sRemark');
                window.top.xhTop.showMessage({
                    title: '[故障详情]',
                    text: sRemark
                });
            });
            //删除
            $('#bt_list_delRepair').click(function(){
                if(xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().delRepair();
            });
            //导出
            $('#bt_list_export').click(function () {
                $this.funObj().exportRepairTablet();
            });
            //修改状态
            $('#bt_list_changeStatus').click(function(){
                if(xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().modifyStatus();
            });
            //上传单据
            $('#bt_list_uploadDoc').click(function(){
                if(xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().uploadDoc();
            });
        },
        funObj: function () {
            var $this = this;
            return {
                //添加
                addRepair: function () {
                    window.top.xhTop.showModal({
                        title: '新增设备维修',
                        content: xhTool.getFrameString('Device/addDeviceRepair', null, 380),
                        button: '保存',
                        size: 'sm',
                        width:'600',
                        backClose: false
                    });
                },
                //接收
                receiveQR: function () {
                    var ids = $this.dataGrid.getSelectValues('iId');
                    if (ids.length <= 0) {
                        window.top.xhTop.showError('未选中记录！');
                        return false;
                    }
                    xhExtra.ajaxRequest('Device/getQR', {ids: ids, type: 1, status: 14}, {check: true}, function(image) {
                        window.top.swal({
                            title: '接收设备'+ ids.length + '台',
                            html: '<img src="' + image.msg + '" width="80%">'
                        });
                    });
                },
                //送修
                deliverRepair: function () {
                    var ids = $this.dataGrid.getSelectValues('iId');
                    var now = new Date();
                    var day = ("0" + now.getDate()).slice(-2);//格式化月，如果小于9，前面补0
                    var month = ("0" + (now.getMonth() + 1)).slice(-2);//拼装完整日期格式
                    var today = now.getFullYear()+"-"+(month)+"-"+(day);
                    if (ids.length <= 0) {
                        window.top.xhTop.showError('未选中记录！');
                        return false;
                    }
                    window.top.swal({
                        showCancelButton: true,
                        confirmButtonText: '确认',
                        cancelButtonText: '取消',
                        showLoaderOnConfirm: true,
                        allowOutsideClick: false,
                        title: '填写送修信息',
                        html: '<div>送修时间：<input type="date" id="sRepairDate" class="swal2-input" style="width: 100%" value="'+today+'">' +
                        '快递公司：<input id="company" class="swal2-input" placeholder="必填">' +
                        '快递单号：<input id="express" class="swal2-input" placeholder="必填">' +
                        '<\div>',
                        preConfirm: function() {
                            return new Promise(function (resolve, reject) {
                                var sRepairDate = $('#sRepairDate', window.top.document).val();
                                var company = $('#company', window.top.document).val();
                                var express = $('#express', window.top.document).val();
                                if(sRepairDate < 1){
                                    reject('请填写送修时间！');
                                } else if(company < 1){
                                    reject('请填写快递公司！');
                                } else if(express < 1){
                                    reject('请填写快递单号！');
                                } else {
                                    xhExtra.ajaxRequest('Device/changeStatus', {
                                        ids: ids,
                                        sRepairDate: sRepairDate,
                                        sendCompany: company,
                                        sendExpress: express,
                                        type: 2,
                                        status: 1
                                    }, {success: true});
                                    resolve();
                                    $this.dataGrid.reload();
                                }
                            })
                        }
                    })
                    // xhExtra.askChangeStatus('Device/changeStatus', {ids: ids, type: 2, status: 1}, $this.dataGrid, '确认送修？');
                },
                //开始检修
                startCheck: function () {
                    var ids = $this.dataGrid.getSelectValues('iId');
                    if (ids.length <= 0) {
                        window.top.xhTop.showError('未选中记录！');
                        return false;
                    }
                    xhExtra.askChangeStatus('Device/changeStatus', {ids: ids, type: 3, status: 2}, $this.dataGrid, '确认开始检修？');
                },

                //检修结果
                setCheckResult: function (level) {
                    var ids = $this.dataGrid.getSelectValue('iId');
                    var sModel = $this.dataGrid.getSelectValue('sModel');
                    if (ids === null) {
                        window.top.xhTop.showError('未选中记录！');
                        return false;
                    }
                    if(sModel === null){
                        window.top.xhTop.showError('无设备数据！');
                        return false;
                    }
                    if (level == 1){//无法维修
                        window.top.swal({
                            input: 'file',
                            inputAttributes: {accept: '.jpg,.jpeg,.png'},
                            showCancelButton: true,
                            confirmButtonText: '确认',
                            cancelButtonText: '取消',
                            showLoaderOnConfirm: true,
                            allowOutsideClick: false,
                            title: '填写检修信息',
                            html: '<div>检测内容：<textarea type="text" id="chectContent" class="swal2-textarea" placeholder="检修内容，必填"></textarea></div>' +
                            '<div>原因：<textarea type="text" id="resion" class="swal2-textarea" placeholder="无法维修原因，必填"></textarea></div>' +
                            '<div>检测报告(jpg/jpeg/png/bmp/gif)：</div>',
                            preConfirm: function(file) {
                                return new Promise(function (resolve, reject) {
                                    var resion = $('#resion', window.top.document).val();
                                    var chectContent = $('#chectContent', window.top.document).val();
                                    if(chectContent < 1){
                                        reject('内容不能为空！');
                                        return false;
                                    } else if(resion < 1){
                                        reject('原因不能为空！');
                                        return false;
                                    } else if (!file) {
                                        reject('文件不能为空！');
                                        return false;
                                    } else {
                                        //上传附件
                                        var fileData = new FormData();
                                        fileData.append('file', file);
                                        $.ajax({
                                            url: baseUrl + 'Device/updateCheckFile',
                                            type: 'POST',
                                            processData: false,
                                            contentType: false,
                                            async: false,//多个ajax按顺序执行
                                            data: fileData,
                                            complete: function (XMLHttpRequest) {
                                                window.top.swal.resetDefaults();
                                                var result = XMLHttpRequest.responseJSON;
                                                if (xhExtra.checkError(result)){
                                                    return false;
                                                }
                                                $this.URL = result.msg;
                                            }
                                        });
                                    }
                                    if ($this.URL < 1){
                                        reject('文件上传错误！');
                                        return false;
                                    }
                                    xhExtra.ajaxRequest('Device/changeStatus', {
                                        ids: ids,
                                        unrepairresion: resion,
                                        chectContent: chectContent,
                                        type: 4,
                                        status: 4,
                                        checkReport: $this.URL
                                    }, {success: true, async: false});//多个ajax按顺序执行
                                    $this.dataGrid.reload();
                                })
                            }
                        })
                    } else if(level == 3){//需收费
                        window.top.xhTop.showModal({
                            title: '设置收费内容',
                            content: xhTool.getFrameString('Device/setCharge', {ids:ids, type: 5, status: 5, model: sModel, free: 1}, 380),
                            button: '保存',
                            size: 'sm',
                            width:'600'
                        });
                    } else {//免费
                        window.top.xhTop.showModal({
                            title: '填写检测内容',
                            content: xhTool.getFrameString('Device/setCharge', {ids:ids, type: 16, status: 6, model: sModel, free: 2}, 380),
                            button: '保存',
                            size: 'sm',
                            width:'600'
                        });
                    }
                },
                //收费沟通
                setCheckPay: function (level) {
                    var ids = $this.dataGrid.getSelectValues('iId');
                    if (ids.length === 0) {
                        window.top.xhTop.showError('未选中记录！');
                        return false;
                    }
                    if (ids.length >1) {
                        window.top.xhTop.showError('请单选记录！');
                        return false;
                    }
                    ids = ids[0];
                    if (level === 2){//不同意
                        window.top.swal({
                            showCancelButton: true,
                            confirmButtonText: '确认',
                            cancelButtonText: '取消',
                            showLoaderOnConfirm: true,
                            allowOutsideClick: false,
                            title: '填写沟通内容/原因',
                            html: '<div>沟通时间：<input type="date" id="commDate" class="swal2-input" style="width: 100%" value="'+xhTool.getNowDate()+'"></div>' +
                            '<div><textarea type="text" id="resion" class="swal2-textarea" placeholder="必填"></textarea></div>',
                            preConfirm: function() {
                                return new Promise(function (resolve, reject) {
                                    var resion     = $('#resion', window.top.document).val();
                                    var commDate     = $('#commDate', window.top.document).val();
                                    if (resion < 1){
                                        reject('内容不能为空');
                                        return false;
                                    } else if (commDate < 1){
                                        reject('请选择沟通时间');
                                        return false;
                                    } else {
                                        xhExtra.ajaxRequest('Device/changeStatus', {
                                            ids: ids,
                                            backresion: resion,
                                            commDate: commDate,
                                            type: 6,
                                            status: 7
                                    }, {success: true});
                                        resolve();
                                        $this.dataGrid.reload();
                                    }
                                })
                            }
                        })
                    } else if (level === 1){//同意，先收费后维修
                        window.top.swal({
                            input: 'file',
                            inputAttributes: {accept: '.jpg,.jpeg,.png'},
                            showCancelButton: true,
                            confirmButtonText: '确认',
                            cancelButtonText: '取消',
                            showLoaderOnConfirm: true,
                            allowOutsideClick: false,
                            title: '填写沟通信息',
                            html: '<div>沟通时间：<input type="date" id="commDate" class="swal2-input" style="width: 100%" value="'+xhTool.getNowDate()+'"></div>' +
                            '<div>沟通内容<textarea type="text" id="commCondition" class="swal2-textarea" placeholder="必填"></textarea></div>' +
                            '<div>运费：<input id="expressCharge" class="swal2-input" placeholder="必填" value="14"></div>' +
                            '<div>上传单据(jpg/jpeg/png/bmp/gif)：</div>',
                            preConfirm: function(file) {
                                return new Promise(function (resolve, reject) {
                                    var commCondition     = $('#commCondition', window.top.document).val();
                                    var expressCharge     = $('#expressCharge', window.top.document).val();
                                    var commDate     = $('#commDate', window.top.document).val();
                                    if(commCondition < 1){
                                        reject('沟通内容不能为空');
                                        return false;
                                    } else if(expressCharge < 1){
                                        reject('运费不能为空！');
                                        return false;
                                    } else if(commDate < 1){
                                        reject('选择沟通时间');
                                        return false;
                                    } else if(!file){
                                        reject('文件不能为空！');
                                        return false;
                                    } else {
                                        //上传附件
                                        var fileData = new FormData();
                                        fileData.append('file', file);
                                        $.ajax({
                                            url: baseUrl + 'Device/updateCheckFile',
                                            type: 'POST',
                                            processData: false,
                                            contentType: false,
                                            async: false,//多个ajax按顺序执行
                                            data: fileData,
                                            complete: function (XMLHttpRequest) {
                                                window.top.swal.resetDefaults();
                                                var result = XMLHttpRequest.responseJSON;
                                                if (xhExtra.checkError(result)){
                                                    return false;
                                                }
                                                $this.URL = result.msg;
                                            }
                                        });

                                        if ($this.URL < 1){
                                            reject('文件上传错误！');
                                            return false;
                                        }
                                        xhExtra.ajaxRequest('Device/changeStatus', {
                                            ids: ids,
                                            backresion: commCondition,
                                            type: 13,
                                            status: 8,
                                            document: $this.URL,
                                            expressCharge: expressCharge,
                                            commDate: commDate
                                        }, {success: true, async: false});
                                        $this.dataGrid.reload();
                                    }
                                })
                            }
                        })
                    } else if (level === 3){//同意，先维修后收费
                        window.top.swal({
                            showCancelButton: true,
                            confirmButtonText: '确认',
                            cancelButtonText: '取消',
                            showLoaderOnConfirm: true,
                            allowOutsideClick: false,
                            title: '填写沟通信息',
                            html: '<div>沟通时间：<input type="date" id="commDate" class="swal2-input" style="width: 100%" value="'+xhTool.getNowDate()+'"></div>' +
                            '<div>沟通内容<textarea type="text" id="commCondition" class="swal2-textarea" placeholder="必填"></textarea></div>' +
                            '<div>运费：<input id="expressCharge" class="swal2-input" placeholder="必填" value="14"></div>',
                            preConfirm: function(file) {
                                return new Promise(function (resolve, reject) {
                                    var commCondition     = $('#commCondition', window.top.document).val();
                                    var expressCharge     = $('#expressCharge', window.top.document).val();
                                    var commDate     = $('#commDate', window.top.document).val();
                                    if(commCondition < 1){
                                        reject('沟通内容不能为空');
                                        return false;
                                    } else if(expressCharge < 1){
                                        reject('运费不能为空！');
                                        return false;
                                    } else if(commDate < 1){
                                        reject('选择沟通时间');
                                        return false;
                                    } else {
                                        xhExtra.ajaxRequest('Device/changeStatus', {
                                            ids: ids,
                                            backresion: commCondition,
                                            type: 16,
                                            status: 18,
                                            expressCharge: expressCharge,
                                            commDate: commDate
                                        }, {success: true, async: false});
                                        $this.dataGrid.reload();
                                    }
                                })
                            }
                        });
                    }
                },
                //确认收费
                confirmPay: function () {
                    var ids = $this.dataGrid.getSelectValues('iId');
                    if (ids.length <= 0) {
                        window.top.xhTop.showError('未选中记录！');
                        return false;
                    }
                    xhExtra.askChangeStatus('Device/changeStatus', {ids: ids, type: 16, status: 13}, $this.dataGrid, '确认收费完成？');
                },
                //开始维修
                startRepair: function () {
                    var ids = $this.dataGrid.getSelectValues('iId');
                    if (ids.length <= 0) {
                        window.top.xhTop.showError('未选中记录！');
                        return false;
                    }
                    xhExtra.askChangeStatus('Device/changeStatus', {ids: ids, type: 7, status: 16}, $this.dataGrid, '确认开始维修？');
                },
                //维修结果
                repairResult: function (level) {
                    var ids = $this.dataGrid.getSelectValues('iId');
                    if (ids.length <= 0) {
                        window.top.xhTop.showError('未选中记录！');
                        return false;
                    }
                    if (level == 1){
                        xhExtra.askChangeStatus('Device/changeStatus', {ids: ids, type: 11, status: 3}, $this.dataGrid, '确认未维修？');
                    } else if(level == 2){
                        xhExtra.askChangeStatus('Device/changeStatus', {ids: ids, type: 10, status: 10}, $this.dataGrid, '确认正常开机？');
                    } else if(level == 3){
                        var now = new Date();
                        var day = ("0" + now.getDate()).slice(-2);//格式化月，如果小于9，前面补0
                        var month = ("0" + (now.getMonth() + 1)).slice(-2);//拼装完整日期格式
                        var today = now.getFullYear()+"-"+(month)+"-"+(day);
                        window.top.swal({
                            showCancelButton: true,
                            confirmButtonText: '确认',
                            cancelButtonText: '取消',
                            showLoaderOnConfirm: true,
                            allowOutsideClick: false,
                            title: '填写重修信息',
                            html: '<div>' +
                            '去向：<select id="backdest" class="swal2-input"><option value="1">公司</option><option value="2">检修站</option></select>' +
                            '快递公司：<input id="backcompany" class="swal2-input" placeholder="选填，选择检修站必填！"></div>' +
                            '快递单号：<input id="backexpress" class="swal2-input" placeholder="选填，选择检修站必填！"></div>' +
                            '送修时间(选检修站请填写)：<input type="date" id="repairDate" class="swal2-input" placeholder="选填，选择检修站必填！" value="'+today+'">' +
                            '故障描述：<textarea type="text" id="backremark" class="swal2-textarea" placeholder="必填"></textarea>' +
                            '</div>',
                            preConfirm: function() {
                                return new Promise(function (resolve, reject) {
                                    var express     = $('#backexpress', window.top.document).val();
                                    var backcompany     = $('#backcompany', window.top.document).val();
                                    var dest     = $('#backdest', window.top.document).val();
                                    var repairDate     = $('#repairDate', window.top.document).val();
                                    var backremark     = $('#backremark', window.top.document).val();
                                    if((dest == 2 && backcompany < 1) || (dest == 2 && express < 1) || (backremark < 1) || (dest == 2 && repairDate < 1)){
                                        reject('请填写完整信息！');
                                    } else {
                                        xhExtra.ajaxRequest('Device/changeStatus', {
                                            ids: ids,
                                            backexpress: express,
                                            backExpressCom: backcompany,
                                            backdest: dest,
                                            sRepairDate: repairDate,
                                            backremark: backremark,
                                            type: 12,
                                            status: 12
                                        }, {success: true});
                                        resolve();
                                        $this.dataGrid.reload();
                                    }
                                })
                            }
                        })
                    }
                },
                //查找
                searchList: function (_return) {
                    var params = {
                        search: {
                            vague: xhExtra.getValue($('#list_vague'))
                        },
                        progress_status: xhExtra.getValue($('#progress_status')),
                        school_area: $('#school_area').combotree('getValue'),
                        repair_project: xhExtra.getValue($('#repair_project')),
                        repair_purpose: xhExtra.getValue($('#repair_purpose')),
                        repair_delay: xhExtra.getValue($('#repair_delay')),
                        repair_commit: xhExtra.getValue($('#repair_commit')),
                        repair_date: $('#repair_date').combobox('getValue'),
                        startTime: $this.start.getElementValue(),
                        endTime: $this.end.getElementValue(),
                        document: xhExtra.getValue($('#repair_document'))
                    };
                if (_return) {
                    params.iIds = $this.dataGrid.getSelectValues('iId');
                    return params;
                }
                    $this.dataGrid.reload(params);
                },
                //维修完成
                endRepair: function () {
                    var ids = $this.dataGrid.getSelectValue('iId');
                    if (ids === null) {
                        window.top.xhTop.showError('未选中记录！');
                        return false;
                    }
                    var project = $this.dataGrid.getSelectValue('sRepairProjectName');
                    var money = $this.dataGrid.getSelectValue('iMoney');
                    var iCheckResult = $this.dataGrid.getSelectValue('iCheckResult');
                    var char = '';
                    if (iCheckResult !== '收费' && iCheckResult !== '免费'){
                         char = '<div>' +
                            '维修内容：<textarea type="text" id="sRepairContent" class="swal2-textarea" placeholder="必填"></textarea>' +
                            '变化信息：<input id="newMessage" class="swal2-input" placeholder="若维修后有变化信息，请填写"></div>';
                    } else {
                        if (project === null){
                            if ( iCheckResult === '收费') {
                                window.top.xhTop.showError('维修项目为空！');
                                return false;
                            }
                            project = '';
                        } else {
                            project = project.split("</i>");
                            if (project.length > 1){
                                project = project[1];
                            } else {
                                project = project[0];
                            }
                        }
                        if (money === null){
                            if ( iCheckResult === '收费'){
                                window.top.xhTop.showError('维修金额为空！');
                                return false;
                            }
                            money = '';
                        }
                         char = '<div>' +
                        '维修项目：<input disabled="disabled" id="swal-input1" class="swal2-input" value="'+ project + '">' +
                        '金额：<input disabled="disabled" id="swal-input2" class="swal2-input" value="'+ money + '">' +
                        '维修内容：<textarea type="text" id="sRepairContent" class="swal2-textarea" placeholder="必填"></textarea>';
                        if (project.indexOf("主板") !== -1){
                            char = char +
                                '新设备号：<input id="newDeviceNumber" class="swal2-input" placeholder="必填"></div>' +
                            '新IMEI：<input id="newImei" class="swal2-input" placeholder="必填"></div>';
                        }
                        char = char + '其他变化信息：<input id="newMessage" class="swal2-input" placeholder="若有其他变化信息，请填写"></div>';
                    }
                    window.top.swal({
                        showCancelButton: true,
                        confirmButtonText: '确认',
                        cancelButtonText: '取消',
                        showLoaderOnConfirm: true,
                        allowOutsideClick: false,
                        title: '填写维修内容',
                        width: 500,
                        padding: 10,
                        html: char,
                        preConfirm: function() {
                            return new Promise(function (resolve, reject) {
                                var repairContent     = $('#sRepairContent', window.top.document).val();
                                var newMessage     = $('#newMessage', window.top.document).val();
                                var newDeviceNumber     = $('#newDeviceNumber', window.top.document).val();
                                var newImei     = $('#newImei', window.top.document).val();
                                if(repairContent < 1 ){
                                    reject('内容不能为空');
                                } else {
                                    if (project !== '' && project.indexOf("主板") !== -1){
                                        if (newDeviceNumber < 1 || newImei < 1){
                                            reject('更新的设备号和Imei不能为空');
                                            return false;
                                        }
                                    }
                                    xhExtra.ajaxRequest('Device/changeStatus', {
                                        ids: ids,
                                        repairContent: repairContent,
                                        newMessage: newMessage,
                                        newDeviceNumber: newDeviceNumber,
                                        newImei: newImei,
                                        type: 8,
                                        status: 9
                                    }, {success: true});
                                    resolve();
                                    $this.dataGrid.reload();
                                }
                            })
                        }
                    })
                },
                //寄回
                sendBack: function () {
                    var ids = $this.dataGrid.getSelectValues('iId');
                    if (ids.length <= 0) {
                        window.top.xhTop.showError('未选中记录！');
                        return false;
                    }
                    var now = new Date();
                    var day = ("0" + now.getDate()).slice(-2);//格式化月，如果小于9，前面补0
                    var month = ("0" + (now.getMonth() + 1)).slice(-2);//拼装完整日期格式
                    var today = now.getFullYear()+"-"+(month)+"-"+(day);
                    window.top.swal({
                        showCancelButton: true,
                        confirmButtonText: '确认',
                        cancelButtonText: '取消',
                        showLoaderOnConfirm: true,
                        allowOutsideClick: false,
                        title: '填写快递信息',
                        html:'<div>寄回时间：<input type="date" id="backDate" class="swal2-input" style="width: 100%" value="'+today+'">' +
                        '<div>快递公司：<input type="text" id="company" class="swal2-input" placeholder="如申通、顺丰等等，必填"></div>' +
                        '<div>快递单号：<input type="text" id="express" class="swal2-input" placeholder="必填"></div>',
                        preConfirm: function() {
                            return new Promise(function (resolve, reject) {
                                var express     = $('#express', window.top.document).val();
                                var company     = $('#company', window.top.document).val();
                                var backDate     = $('#backDate', window.top.document).val();
                                if(company < 1){
                                    reject('快递公司不能为空！');
                                } else if(express < 1) {
                                    reject('快递号不能为空！');
                                } else if(backDate < 1) {
                                    reject('时间不能为空！');
                                }else {
                                    xhExtra.ajaxRequest('Device/changeStatus', {
                                        ids: ids,
                                        express: express,
                                        backExpCompany: company,
                                        backDate: backDate,
                                        type: 9,
                                        status: 11
                                    }, {success: true});
                                    resolve();
                                    $this.dataGrid.reload();
                                }
                            })
                        }
                    })
                },
                //历史记录
                history: function () {
                    var id = $this.dataGrid.getSelectValue('iId');
                    var sUserName = $this.dataGrid.getSelectValue('sUserName');
                    var sModel = $this.dataGrid.getSelectValue('sModel');
                    var sDeviceNumber = $this.dataGrid.getSelectValue('sDeviceNumber');
                    var sBrand = $this.dataGrid.getSelectValue('sBrand');
                    if (id === null) {
                        window.top.xhTop.showError('未选中记录！');
                        return false;
                    }
                    window.top.xhTop.showModal({
                        title: '操作记录:'+sUserName+'-'+sModel+'-'+sDeviceNumber+'-'+sBrand,
                        content: xhTool.getFrameString('Device/repairHandleList', {id:id}, 500),
                        // button: '保存',
                        size: 'lg',
                        width:'800'
                    });
                },
                repairDelay: function () {
                    var id = $this.dataGrid.getSelectValue('iId');
                    var sUserName = $this.dataGrid.getSelectValue('sUserName');
                    var sModel = $this.dataGrid.getSelectValue('sModel');
                    var sDeviceNumber = $this.dataGrid.getSelectValue('sDeviceNumber');
                    var sBrand = $this.dataGrid.getSelectValue('sBrand');
                    if (id === null) {
                        window.top.xhTop.showError('未选中记录！');
                        return false;
                    }
                    window.top.xhTop.showModal({
                        title: '延时记录:'+sUserName+'-'+sModel+'-'+sDeviceNumber+'-'+sBrand,
                        content: xhTool.getFrameString('Device/repairDelayList', {id:id}, 300),
                        // button: '保存',
                        size: 'md',
                        width:'800'
                    });
                },
                //接收
                bt_list_takeAwayQR: function () {
                    var ids = $this.dataGrid.getSelectValues('iId');
                    if (ids.length <= 0) {
                        window.top.xhTop.showError('未选中记录！');
                        return false;
                    }
                    xhExtra.ajaxRequest('Device/getQR', {ids: ids, type: 15, status: 15}, {check: true}, function(image) {
                        window.top.swal({
                            title: '领取设备'+ ids.length + '台',
                            html: '<img src="' + image.msg + '" width="80%">'
                        });
                    });
                },
                //删除
                delRepair: function () {
                    var ids = $this.dataGrid.getSelectValues('iId');
                    if (ids.length <= 0) {
                        window.top.xhTop.showError('未选中记录！');
                        return false;
                    }
                    xhExtra.askChangeStatus('Device/delRepairApply', {ids: ids}, $this.dataGrid, '确认删除？');
                },
                getRepairStat: function (level) {
                    if (level === 1){
                        name = '状态';
                        type = 1;
                    } else {
                        name = '维修项目';
                        type = 2;
                    }
                    window.top.xhTop.showModal({
                        title: '按' + name + '统计：',
                        content: xhTool.getFrameString('Device/getRepairStat', {type: type}, 500),
                        size: 'md',
                        button: '导出'
                    });
                },
                exportRepairTablet: function () {
                    location.href = baseUrl + 'device/exportRepairData?export=true&' + xhTool.parseParam($.extend({}, $this.funObj().searchList(true), $this.params));
                },
                commData: function (level) {
                    var id = $this.dataGrid.getSelectValues('iId');
                    var sDeviceNumber = $this.dataGrid.getSelectValue('sDeviceNumber');
                    if (id.length <= 0) {
                        window.top.xhTop.showError('未选中记录！');
                        return false;
                    }
                    if (id.length > 1){
                        window.top.xhTop.showError('请单选后操作');
                        return false;
                    }
                    if (level === 1){
                        window.top.swal({
                            showCancelButton: true,
                            confirmButtonText: '确认',
                            cancelButtonText: '取消',
                            showLoaderOnConfirm: true,
                            allowOutsideClick: false,
                            title: '填写沟通信息',
                            html: '<div>沟通时间：<input type="date" id="commDate" class="swal2-input" style="width: 100%" value="'+xhTool.getNowDate()+'"></div>' +
                            '<div>沟通内容<textarea type="text" id="commContent" class="swal2-textarea" placeholder="必填"></textarea></div>',
                            preConfirm: function() {
                                return new Promise(function (resolve, reject) {
                                    var commDate     = $('#commDate', window.top.document).val();
                                    var commContent     = $('#commContent', window.top.document).val();
                                    if(commDate < 1){
                                        reject('日期不能为空');
                                        return false;
                                    } else if(commContent === '') {
                                        reject('请填写沟通内容');
                                        return false;
                                    } else {
                                        xhExtra.ajaxRequest('Device/setCommData', {
                                            id: id[0],
                                            commDate: commDate,
                                            commContent: commContent
                                        }, {success: true});
                                        resolve();
                                        $this.dataGrid.reload();
                                    }
                                })
                            }
                        })
                    } else {
                        window.top.xhTop.showModal({
                            title: '设备' + sDeviceNumber + '沟通记录：',
                            content: xhTool.getFrameString('Device/getCommHistory', {id: id[0]}, 500),
                            size: 'md',
                            button: true
                        });
                    }
                },
                modifyCharge: function () {
                    var id = $this.dataGrid.getSelectValues('iId');
                    if (id.length <= 0) {
                        window.top.xhTop.showError('未选中记录！');
                        return false;
                    }
                    if (id.length > 1){
                        window.top.xhTop.showError('请单选后操作');
                        return false;
                    }
                    var iCheckResult = $this.dataGrid.getSelectValue('iCheckResult');
                    var iConfirmCharge = $this.dataGrid.getSelectValue('iConfirmCharge');
                    var iAgreeChargeName = $this.dataGrid.getSelectValue('iAgreeChargeName');
                    if (iCheckResult === '收费' && iAgreeChargeName !== '不同意' && (iConfirmCharge === '否' || iConfirmCharge === '')) {
                        window.top.xhTop.showModal({
                            title: '修改收费内容',
                            content: xhTool.getFrameString('Device/modifyCharge', {id:id}, 250),
                            button: '保存',
                            size: 'sm',
                            backClose: false,
                            width:'600'
                        });
                    } else {
                        window.top.xhTop.showError('该设备目前状态无法修改，请重新选择操作');
                        return false;
                    }
                },
                modifyStatus: function () {
                    var id = $this.dataGrid.getSelectValues('iId');
                    var options_str = '';
                    xhExtra.ajaxRequest('device/getStatusArr', null, {success: false,check:false, async: false}, function (data) {
                        $.each(data, function (key, value) {
                            options_str = options_str + '<option value="' + key + '">' + value + '</option>';
                        });
                    });
                    window.top.swal({
                        showCancelButton: true,
                        confirmButtonText: '确认',
                        cancelButtonText: '取消',
                        showLoaderOnConfirm: true,
                        allowOutsideClick: false,
                        title: '选择状态',
                        html: '<div>' +
                        '<select id="status" class="swal2-input">' +
                        options_str +
                        '</select>' +
                        '</div>',
                        preConfirm: function() {
                            return new Promise(function (resolve, reject) {
                                var status = $('#status', window.top.document).val();
                                    xhExtra.ajaxRequest('Device/modifyStatus', {
                                        id: id,
                                        statusCode: status,
                                        status: 17
                                    }, {success: true});
                                    resolve();
                                    $this.dataGrid.reload();
                            })
                        }
                    })
                },
                uploadDoc: function () {
                    var id = $this.dataGrid.getSelectValue('iId');
                    var chargeType = $this.dataGrid.getSelectValue('iChargeType');
                    if (id === null) {
                        window.top.xhTop.showError('未选中记录！');
                        return false;
                    }
                    if (chargeType === '0'){
                        window.top.xhTop.showError('记录不符合上传条件，请重新选择');
                        return false;
                    }
                    window.top.swal({
                        input: 'file',
                        inputAttributes: {accept: '.jpg,.jpeg,.png,.bmp,.gif'},
                        showCancelButton: true,
                        confirmButtonText: '确认',
                        cancelButtonText: '取消',
                        showLoaderOnConfirm: true,
                        allowOutsideClick: false,
                        title: '上传检测报告',
                        html: '<div>（支持格式：jpg/jpeg/png/bmp/gif）</div>',
                        preConfirm: function(file) {
                            return new Promise(function (resolve, reject) {
                                    //上传附件
                                    var fileData = new FormData();
                                    fileData.append('file', file);
                                window.top.swal.disableButtons();
                                    $.ajax({
                                        url: baseUrl + 'Device/updateCheckFile',
                                        type: 'POST',
                                        processData: false,
                                        contentType: false,
                                        async: false,//多个ajax按顺序执行
                                        data: fileData,
                                        complete: function (XMLHttpRequest) {
                                            window.top.swal.resetDefaults();
                                            var result = XMLHttpRequest.responseJSON;
                                            if (xhExtra.checkError(result)){
                                                return false;
                                            }
                                            $this.URL = result.msg;
                                        }
                                    });
                                if ($this.URL < 1){
                                    reject('文件上传错误！');
                                    window.top.swal.enableButtons();
                                    return false;
                                }
                                xhExtra.ajaxRequest('Device/modifyDocUrl', {
                                    id: id,
                                    docUrl: $this.URL
                                }, {success: true, async: false},window.top.swal.enableButtons());//多个ajax按顺序执行
                                $this.dataGrid.reload();
                            })
                        }
                    })
                }
            }
        }
    }
}